package com.example.seckilldemo.config;

import com.example.seckilldemo.entity.TUser;
import com.example.seckilldemo.service.ITUserService;
import com.example.seckilldemo.utils.CookieUtil;
import com.example.seckilldemo.vo.RespBean;
import com.example.seckilldemo.vo.RespBeanEnum;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.mapstruct.BeforeMapping;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.spi.http.HttpExchange;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.concurrent.TimeUnit;

//@Component
//@Aspect
//@Slf4j
//
//
//public class Acessaspect {
//
////    private static final Logger log = LoggerFactory.getLogger(AccessLimit.class);
//    @Autowired
//    RedisTemplate redisTemplate;
//
//
//    @Autowired
//    private ITUserService itUserService;
//
//     @Before("@annotation(accessLimit)")
//    public void dobefore(JoinPoint point, AccessLimit accessLimit) throws IOException {
//         HttpServletRequest request= ( (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
//         HttpServletResponse response=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
//
//         TUser tUser = getUser(request,response);
//         UserContext.setUser(tUser);
//
//         int second = accessLimit.second();
//         int maxCount = accessLimit.maxCount();
//         boolean needLogin = accessLimit.needLogin();
//
//         System.out.println("haha");
//         log.info("ha");
//         String key = request.getRequestURI();
//         if (needLogin) {
//             if (tUser == null) {
//                 render(response, RespBeanEnum.SESSION_ERROR);
//             }
//             key += ":" + tUser.getId();
//         }
//         ValueOperations valueOperations = redisTemplate.opsForValue();
//         Integer count = (Integer) valueOperations.get(key);
//         if (count == null) {
//             valueOperations.set(key, 1, second, TimeUnit.SECONDS);
//         } else if (count < maxCount) {
//             valueOperations.increment(key);
//         } else {
//             render(response, RespBeanEnum.ACCESS_LIMIT_REACHED);
//
//         }
//     }
//    private TUser getUser(HttpServletRequest request,HttpServletResponse response) {
//
//        String userTicket = CookieUtil.getCookieValue(request, "userTicket");
//        if (StringUtils.isEmpty(userTicket)) {
//            return null;
//        }
//        return itUserService.getUserByCookie(userTicket, request, response);
//    }
//    private void render(HttpServletResponse response, RespBeanEnum respBeanEnum) throws IOException {
//        response.setCharacterEncoding("UTF-8");
//        response.setContentType("application/json");
//        PrintWriter printWriter = response.getWriter();
//        RespBean bean = RespBean.error(respBeanEnum);
//        printWriter.write(new ObjectMapper().writeValueAsString(bean));
//        printWriter.flush();
//        printWriter.close();
//    }
//
//    }




